﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plat.WfProcess.Common;
using Plat.WfProcess.DataModel;

namespace Plat.WfProcess.Business
{
    /// <summary>
    /// 任务管理类：包括任务及任务视图对象
    /// </summary>
    public class TaskManager
    {
        #region TaskManager 属性列表
        private TaskRepository _taskRepository;
        private TaskRepository taskRepository
        {
            get
            {
                if (_taskRepository == null)
                {
                    _taskRepository = new TaskRepository();
                }
                return _taskRepository;
            }
        }

        private TaskViewRepository _taskViewRepository;
        private TaskViewRepository taskViewRepository
        {
            get
            {
                if (_taskViewRepository == null)
                {
                    _taskViewRepository = new TaskViewRepository();
                }
                return _taskViewRepository;
            }
        }

        #endregion

        #region TaskManager 任务分配视图
        public TaskViewEntity Select(long taskID)
        {
            return taskViewRepository.SelectById(taskID.ToString());
        }

        /// <summary>
        /// 按条件查询任务列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="allRowsCount"></param>
        /// <param name="where"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public IList<TaskViewEntity> Select(int pageIndex,
            int pageSize,
            string orderBy,
            out long allRowsCount,
            string where,
            params object[] values)
        {
            return taskViewRepository.Select(pageIndex, pageSize, out allRowsCount, orderBy, false, where, values);
        }


        /// <summary>
        /// 获取一个流程定义下的所有任务实例
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="allRowsCount"></param>
        /// <param name="applicationInstanceID"></param>
        /// <returns></returns>
        internal IList<TaskViewEntity> Select(long applicationInstanceID,
            string processGUID)
        {
            long allRowsCount;
            return Select(0, int.MaxValue, "TaskID", out allRowsCount, "ApplicationInstanceID == @0 and ProcessGUID.ToString() == @1", 
                applicationInstanceID,
                processGUID);
        }

        /// <summary>
        /// 获取流程实例下的任务数据
        /// </summary>
        /// <param name="applicationInstanceID">应用ID</param>
        /// <param name="processInstanceGUID">流程实例ID</param>
        /// <returns>任务列表数据</returns>
        internal IList<TaskViewEntity> SelectProcessTasks(long applicationInstanceID,
            string processInstanceGUID)
        {
            long allRowsCount;
            return Select(0, int.MaxValue, "TaskID", out allRowsCount, "ApplicationInstanceID == @0 and ProcessInstanceGUID.ToString() == @1",
                applicationInstanceID,
                processInstanceGUID);
        }

        internal IList<TaskViewEntity> SelectProcessTasksWithState(long applicationInstanceID,
            string processInstanceGUID,
            NodeStateEnum state)
        {
            long allRowsCount;
            return Select(0, int.MaxValue, "TaskID", out allRowsCount, "ApplicationInstanceID == @0 and ProcessInstanceGUID.ToString() == @1 and ActivityState == @2",
                applicationInstanceID,
                processInstanceGUID,
                (short)state);
        }



        /// <summary>
        /// 获取当前用户的待办任务列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="allRowsCount"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        internal IList<TaskViewEntity> SelectToDoTaskList(int pageIndex,
            int pageSize,
            string orderBy,
            out long allRowsCount,
            long userID)
        {
            return Select(pageIndex, pageSize, orderBy, out allRowsCount, 
                "AssignedToUserID == @0 and IsActivityCompleted == 0 and RecordStatusInvalid == 0", 
                userID);
        }

        /// <summary>
        /// 获取已经办理完成的任务列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="allRowsCount"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        internal IList<TaskViewEntity> SelectDoneTaskList(int pageIndex,
            int pageSize,
            string orderBy,
            out long allRowsCount,
            long userID)
        {
            return Select(pageIndex, pageSize, orderBy, out allRowsCount, 
                "AssignedToUserID == @0 and IsActivityCompleted == 1 and IsTaskCompleted == 1", 
                userID);
        }

        /// <summary>
        /// 判断当前用户，是否有权限办理任务
        /// </summary>
        /// <param name="applicationInstanceID"></param>
        /// <param name="currentUserID"></param>
        /// <returns></returns>
        internal bool GetUserPermission(long applicationInstanceID,
            long currentUserID)
        {
            long allRowsCount;
            IList<TaskViewEntity> taskToDoList = Select(0, 1, string.Empty, out allRowsCount,
                "AssignedToUserID == @0 and IsActivityCompleted == 0 and RecordStatusInvalid == 0",
                currentUserID);

            return (taskToDoList.Count == 1);
        }
        #endregion

        #region TaskManager 任务数据基本操作
        internal TaskEntity SelectInnerTask(long taskID)
        {
            return taskRepository.SelectById(taskID.ToString());
        }

        internal IList<TaskEntity> SelectInnerTask(int pageIndex,
            int pageSize,
            string orderBy,
            out long allRowsCount,
            string where,
            params object[] values)
        {
            return taskRepository.Select(pageIndex, pageSize, out allRowsCount, orderBy, false, where, values);
        }

        /// <summary>
        /// 插入任务数据
        /// </summary>
        /// <param name="entity">任务实体</param>
        /// <param name="wfLinqDataContext">linq上下文</param>
        internal void Insert(TaskEntity entity, 
            ProcessModelDataContext wfLinqDataContext)
        {
            if (wfLinqDataContext != null)
            {
                taskRepository.LinqDataContext = wfLinqDataContext;
            }

            taskRepository.Insert(entity);
        }

        /// <summary>
        /// 插入任务数据
        /// </summary>
        /// <param name="activityInstance"></param>
        /// <param name="performers"></param>
        /// <param name="wfLinqDataContext"></param>
        internal void Insert(ActivityInstanceEntity activityInstance,
            PerformerList performers, 
            ProcessModelDataContext wfLinqDataContext)
        {
            foreach (Performer performer in performers)
            {
                TaskEntity entity = new TaskEntity();
                entity.ActivityInstanceGUID = activityInstance.ActivityInstanceGUID;
                entity.ProcessInstanceGUID = activityInstance.ProcessInstanceGUID;
                entity.TaskTypeID = (short)TaskTypeEnum.Normal;
                entity.AssignedToUserID = performer.CompanyUserID;
                entity.AssignedToUserName = performer.CompanyUserName;
                entity.IsTaskCompleted = 0;
                Insert(entity, wfLinqDataContext);
            }
        }

        /// <summary>
        /// 更新任务数据
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="wfLinqDataContext"></param>
        internal void Update(TaskEntity entity,
            ProcessModelDataContext wfLinqDataContext)
        {
            if (wfLinqDataContext != null)
            {
                taskRepository.LinqDataContext = wfLinqDataContext;
            }
            taskRepository.Update(entity);
        }

        /// <summary>
        /// 设置任务完成
        /// </summary>
        /// <param name="taskID">任务ID</param>
        /// <param name="currentLogonUser"></param>
        /// <param name="wfLinqDataContext"></param>
        internal void Complete(long taskID,
            WfLogonUser currentLogonUser,
            ProcessModelDataContext wfLinqDataContext)
        {
            TaskEntity task = taskRepository.SelectById(taskID.ToString());
            task.IsTaskCompleted = 1;
            task.EndedDateTime = DateTime.Now;
            task.EndedByUserID = currentLogonUser.UserID;
            task.EndedByUserName = currentLogonUser.UserName;

            Update(task, wfLinqDataContext);
        }

        /// <summary>
        /// 任务删除
        /// </summary>
        /// <param name="taskID">任务ID</param>
        internal void Delete(long taskID)
        {
            taskRepository.Delete(taskID.ToString());
        }
        #endregion

    }
}
